package com.zry.yard.view

import android.app.Activity
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.Gravity
import android.widget.LinearLayout
import com.zry.yard.R
import com.zry.yard.animation.RecyclerViewUtilKt


class IndustryTypePopup(context: Activity?, linearLayout: LinearLayout) : BasePopup(context!!, R.layout.industry_type_popup, linearLayout) {
    private var recyclerViewUtilKt01: RecyclerViewUtilKt? = null
    private var recyclerViewUtilKt02: RecyclerViewUtilKt? = null
    private var leftPopupRecycler: RecyclerView? = null
    private var rightPopupRecycler: RecyclerView? = null
    private var firstID: String = ""
    private var firstName: String = ""
    private var secondID: String = ""
    private var secondName: String = ""

    private var firstAdapter: FirstAdapter? = null
    private var secondAdapter: SecondAdapter? = null
    private var firstList: MutableList<DocumentStatusBean>? = null
    private var secondList: MutableList<DocumentStatusBean>? = null

    init {




        leftPopupRecycler = contentView!!.findViewById(R.id.leftPopupRecycler)
        rightPopupRecycler = contentView!!.findViewById(R.id.rightPopupRecycler)
        recyclerViewUtilKt01 = RecyclerViewUtilKt(context!!, leftPopupRecycler!!)
        recyclerViewUtilKt02 = RecyclerViewUtilKt(context, rightPopupRecycler!!)
        recyclerViewUtilKt01?.initRecyclerView()
        recyclerViewUtilKt02?.initRecyclerView()

        firstList= mutableListOf()
        secondList= mutableListOf()
        firstList = PopupUtils().getIndustryType()
        secondList = PopupUtils().getEnterpriseType(0)

        firstAdapter = FirstAdapter(R.layout.item_document_status_popup, firstList!!)
        secondAdapter = SecondAdapter(R.layout.item_document_status_popup, secondList!!)
        recyclerViewUtilKt01?.setAdapter(firstAdapter!!)
        recyclerViewUtilKt02?.setAdapter(secondAdapter!!)

        firstAdapter?.setOnItemClickListener { adapter, view, position ->
            firstID = firstList!![position].documentID
            firstName = firstList!![position].documentName

            secondList = PopupUtils().getEnterpriseType(position)
            secondAdapter = SecondAdapter(R.layout.item_document_status_popup, secondList!!)
            secondAdapter?.notifyDataSetChanged()
            recyclerViewUtilKt02?.setAdapter(secondAdapter!!)

            adapterClick()
        }

        adapterClick()
    }

    private fun adapterClick(){
        secondAdapter?.setOnItemClickListener { adapter, view, position ->
            secondID = secondList!![position].documentID
            secondName = secondList!![position].documentName

            if (listener != null) {
                listener?.handleResult(firstID, firstName, secondID, secondName)
            }

            popupWindow?.dismiss()
        }
    }

    private var listener: IndustryTypeListener? = null

    interface IndustryTypeListener {
        fun handleResult(firstID: String, firstName: String, secondID: String, secondName: String)
    }

    fun setListener(listener: IndustryTypeListener): IndustryTypePopup {
        this.listener = listener
        return this
    }
}